package com.satoken.controller;

import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/auth")
public class AuthenticationController {

    // 获取所有权限 http://localhost:8081/auth/getPermissionList
    @GetMapping("/getPermissionList")
    public List<String> getPermissionList(){
        return StpUtil.getPermissionList();
    }

    // 权限判断 http://localhost:8081/auth/hasPermission
    @GetMapping("/hasPermission")
    public boolean hasPermission(){
        // 判断：当前账号是否含有指定权限, 返回 true 或 false
        return StpUtil.hasPermission("user.add");
    }

    // 权限检查 http://localhost:8081/auth/checkPermission
    @GetMapping("/checkPermission")
    public boolean checkPermission(){
        // 校验：当前账号是否含有指定权限, 如果验证未通过，则抛出异常: NotPermissionException
        StpUtil.checkPermission("user.xadd");
        return true;
    }

    // 权限检查 http://localhost:8081/auth/checkPermissionAnd
    @GetMapping("/checkPermissionAnd")
    public boolean checkPermissionAnd(){
        // 校验：当前账号是否含有指定权限 [指定多个，必须全部验证通过]
        StpUtil.checkPermissionAnd("user.add", "user.add", "art.get");
        return true;
    }

    // http://localhost:8081/auth/getRoleList
    @GetMapping("/getRoleList")
    public List<String> getRoleList(){
        // 获取：当前账号所拥有的角色集合
        return StpUtil.getRoleList();
    }

    // http://localhost:8081/auth/hasRole?role=
    @GetMapping("/hasRole")
    public boolean hasRole(String role){
        return StpUtil.hasRole(role);
    }

    // http://localhost:8081/auth/checkRole?role=
    @GetMapping("/checkRole")
    public boolean checkRole(String role){
        StpUtil.checkRole(role);
        return true;
    }

}
